#ifndef __TOYM__NET__TIMER_H__
#define __TOYM__NET__TIMER_H__

#include <atomic>
#include "Base/Timestamp.h"
#include "net/Callables.h"
#include "net/TimerId.h"
#include "Base/noncopyable.h"

namespace toym
{
namespace net
{

class Timer: noncopyable {
public:
    Timer(Timestamp when, double interval = 0.0);

    void setCallback(const TimerCallback_t& cb) {  m_cb = cb; }
    void setCallback(TimerCallback_t&& cb) { m_cb = std::move(cb); }

    void run() const { if(m_cb) m_cb(); }

    Timestamp getExpired() const { return m_when; }
    bool getRepeat() const { return m_repeat; }
    int64_t getSeq() const { return m_seq; }

    void restart();
    TimerId getId() { return TimerId(this, m_seq); }

    static
    int64_t getCreated() { return s_seq; }
    
private:
    Timestamp m_when;
    double m_interval;
    bool m_repeat;
    TimerCallback_t m_cb;
    int64_t m_seq;

    static std::atomic_int64_t s_seq;
};


} // namespace net

} // namespace toym


#endif //__TOYM__NET__TIMER_H__